package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.DepartmentParam;
import com.dmap.admin.controller.backgroundv2.param.DepartmentSelectParam;
import com.dmap.admin.dao.DepartmentMapper;
import com.dmap.admin.po.Department;
import com.dmap.admin.po.DepartmentExample;
import com.dmap.admin.service.backgroundv2.DepartmentService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 16/05/18
 * \* Description:部门相关接口实现类
 * \
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public Department insertDepartment(DepartmentParam param) {
        Department department = new Department();
        department.setName(param.getName());
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())) {
            department.setCompanyId(Long.valueOf(param.getCompanyId()));
        }
        department.setCompanyName(param.getCompanyName());
        department.setCompanyType(param.getCompanyType());
        if (StringUtils.isNotBlank(param.getCreatorId()) && StringUtils.isNumeric(param.getCreatorId())) {
            department.setCreatorId(Long.valueOf(param.getCreatorId()));
        }
        department.setDescription(param.getDescription());
        department.setStatus(AdminConstant.NORMAL_STATUS);
        department.setCreateTime(System.currentTimeMillis() / 1000);
        department.setUpdateTime(department.getCreateTime());
        int success = departmentMapper.insertSelective(department);
        return (success == 1) ? department : null;
    }

    @Override
    public boolean updateDepartment(DepartmentParam param) {
        Department department = new Department();
        department.setId(Long.valueOf(param.getId()));
        department.setName(param.getName());
        if (StringUtils.isNotBlank(param.getCompanyId()) && StringUtils.isNumeric(param.getCompanyId())) {
            department.setCompanyId(Long.valueOf(param.getCompanyId()));
        }
        department.setCompanyName(param.getCompanyName());
        department.setCompanyType(param.getCompanyType());
        department.setDescription(param.getDescription());
        department.setStatus(param.getStatus());
        department.setUpdateTime(System.currentTimeMillis() / 1000);
        return departmentMapper.updateByPrimaryKeySelective(department) == 1;
    }

    @Override
    public boolean deleteByIdList(List<Long> idList) {
        DepartmentExample example = new DepartmentExample();
        example.createCriteria().andIdIn(idList);
        int success = departmentMapper.deleteByExample(example);
        return success >= 1;
    }

    @Override
    public Department selectById(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean validateNameAndCompanyIdIsExist(String name, String companyId) {
        boolean returnValue = true;
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name).andCompanyIdEqualTo(Long.valueOf(companyId));
        List<Department> list = departmentMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            returnValue = false;
        }
        return returnValue;
    }

    @Override
    public int countByDepartmentSelectParam(DepartmentSelectParam param) {
        DepartmentExample example = this.getDepartmentSelectParamExample(param);
        return departmentMapper.countByExample(example);
    }

    @Override
    public List<Department> selectByDepartmentSelectParam(DepartmentSelectParam param) {
        DepartmentExample example = this.getDepartmentSelectParamExample(param);
        //根据前端所传参数不同生成对应的orderBy子句
        String orderByClause = AdminConstant.getOrderByClauseMap().getOrDefault(param.getSortField(), AdminConstant.CREATE_TIME)
                + AdminConstant.getOrderByClauseSequenceMap().getOrDefault(param.getOrder(), AdminConstant.DESC);
        example.setOrderByClause(orderByClause);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return departmentMapper.selectByExample(example);
    }

    private DepartmentExample getDepartmentSelectParamExample(DepartmentSelectParam param) {
        DepartmentExample example = new DepartmentExample();
        DepartmentExample.Criteria criteria = example.createCriteria();
        //根据公司ID查询
        criteria.andCompanyIdEqualTo(Long.valueOf(param.getCompanyId()));
        //如果用户类型为公司管理员 并且 searchDepartmentId 字段不为空，则根据部门ID查询当前一个部门
        if (param.getUserType() == AdminConstant.USER_TYPE_COMPANY_ADMIN && StringUtils.isNotBlank(param.getSearchDepartmentId())
                && StringUtils.isNumeric(param.getSearchDepartmentId()) && !Long.valueOf(param.getSearchDepartmentId()).equals(0L)) {
            criteria.andIdEqualTo(Long.valueOf(param.getSearchDepartmentId()));
        }
        //如果用户类型为部门管理员 并且 departmentId 字段不为空，则根据部门ID查询当前一个部门
        if (param.getUserType() == AdminConstant.USER_TYPE_DEPARTMENT_ADMIN && StringUtils.isNotBlank(param.getDepartmentId())
                && StringUtils.isNumeric(param.getDepartmentId()) && !Long.valueOf(param.getDepartmentId()).equals(0L)) {
            criteria.andIdEqualTo(Long.valueOf(param.getDepartmentId()));
        }
        //根据 部门名称 模糊搜索
        if (StringUtils.isNotBlank(param.getSearchKeyword())) {
            criteria.andNameLike("%" + param.getSearchKeyword() + "%");
        }
        return example;
    }

}